#pragma ance
#include "Procotol.hpp"
#include <iostream>

enum CalError
{
    divide_by_zero = 1,
    modulo_by_zero,
    unknown_operation
};
CalError Divide = divide_by_zero;
CalError Modulo = modulo_by_zero;
CalError Unknown = unknown_operation;

struct ServerCal
{
    Response PerformCal(Request &req)
    {
        Response res;
        switch (req._op)
        {
        case '+':
            res._answer = req._x + req._y;
            break;
        case '-':
            res._answer = req._x - req._y;
            break;
        case '*':
            res._answer = req._x * req._y;
            break;
        case '/':
            if (req._y == 0)
                res._code = Divide;
            else
                res._answer = req._x / req._y;
            break;
        case '%':
            if (req._y == 0)
                res._code = Modulo;
            else
                res._answer = req._x % req._y;
            break;
        default:
            res._code = Unknown;
            break;
        }

        return res;
    }

    std::string Calculate(std::string& text)
    {
        //协议
        std::string content;
        if(!CustomProtoco::Decode(text, content))
            return "";
        
        //反序列化
        Request req;
        if(!req.deserialize(content))
            return "";

        //完成计算
        Response res = PerformCal(req);

        //序列化
        std::string ret_content = res.serialize();
        //协议
        std::string ret_text = CustomProtoco::Encode(ret_content);

        return ret_text;

        return "";
    }

};